// const modules = import.meta.glob('@/pages/**/index.vue'); 
const modules = import.meta.glob([
    '@/pages/**/*.vue',
    '!@/pages/**/*components*',
    '!@/pages/**/*==*',
    '!@/pages/**/*NotFound*',
    '!@/pages/**/* copy*'
]);
const 父layout = () => import('@/layouts/父layout.vue')

async function 成自动routes() {
    let 自动routes = [];
    const 父路由 = {};
    // 遍历所有匹配的模块
    for (const [key, component] of Object.entries(modules)) {
        const filePath = key.replace('/src/pages/', '');
        const pathParts = filePath.split('/');
        const fileName = pathParts.at(-1).replace('.vue', '');
        // 尝试加载组件并获取其元信息
        let icon, title, name
        try {
            const { default: loadedComponent } = await component();
            icon = loadedComponent.icon || 'school';
            title = loadedComponent.title || pathParts.length > 0 ? pathParts[pathParts.length - 1] : fileName;
            // console.log(`title 👉`, title)
            name = loadedComponent.name || filePath.replace('.vue', '');
        } catch (error) {
            console.error(`Error importing ${key}:`, error);
            continue;
        }

        // 处理非末级路由
        let currentPath = '';
        // 在后续的循环里，currentRoutes 会被重新赋值为父路由的 children 数组👇
        let currentRoutes = 自动routes;
        for (let i = 0; i < pathParts.length - 1; i++) {
            const part = pathParts[i];
            currentPath += `/${part}`;
            if (!父路由[currentPath]) {
                const parentRoute = {
                    path: currentPath,
                    component: 父layout,
                    meta: {
                        icon: 'matArrowDownward',
                        title: part
                    },
                    children: []
                };
                currentRoutes.push(parentRoute);
                父路由[currentPath] = parentRoute;
            }
            currentRoutes = 父路由[currentPath].children;
        }

        // 处理末级路由
        const finalPath = currentPath + `/${fileName}`;
        const finalRoute = {
            path: finalPath,
            component,
            meta: {
                icon,
                title
            },
            name
        };
        currentRoutes.push(finalRoute);
    }
    console.log(`自动routes 👉`, 自动routes)
    return 自动routes;
}

const 自动routes = await 成自动routes();


const routes = [
    {
        path: '/',
        name: 'layout',
        component: () => import('@/layouts/layout.vue'),
        children: 自动routes
    },
    // Always leave this as last one,
    // but you can also remove it
    {
        path: '/:catchAll(.*)*',
        component: () => import('@/pages/ErrorNotFound.vue')
    }
];

export default routes;